package com.qa.controller;

import com.github.pagehelper.PageInfo;
import com.qa.common.ServerResponse;
import com.qa.pojo.Category;
import com.qa.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @Author
 * @Date 2020/3/3 21:17
 * @Desc
 **/
@Controller
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;


    @GetMapping("/cms/category/page")
    public String categoryPage() {
        return "categoryList";
    }


    @GetMapping("/cms/category/list")
    @ResponseBody
    public ServerResponse userList(@RequestParam(value = "userEmail", defaultValue = "") String categoryName,
                                   @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "limit", defaultValue = "10") Integer pageSize) {
        PageInfo<Category> pageInfo = categoryService.getAll(categoryName, pageNum, pageSize);
        return ServerResponse.createBySuccess("查询成功！", pageInfo.getList(), pageInfo.getTotal());
    }

    @GetMapping("/cms/category/detail")
    public String categoryDetail(Model model, @RequestParam("categoryId") Integer categoryId) {
        Category category = categoryService.getById(categoryId);
        model.addAttribute("category", category);
        return "categoryDetail";
    }



    @PostMapping("/cms/category/edit")
    @ResponseBody
    public ServerResponse categoryEdit(@RequestBody Category category) {
        return categoryService.save(category);
    }


    @GetMapping("/cms/category/addPage")
    public String categoryAddPage(){
        return "categoryAddPage";
    }


    @GetMapping("/cms/category/delete")
    @ResponseBody
    public ServerResponse deleteById(@RequestParam("categoryId") Integer categoryId) {
        return categoryService.deleteById(categoryId);
    }

}
